package cn.bdqn.layui.controller;

import cn.bdqn.layui.entity.Menu;
import cn.bdqn.layui.entity.Role;
import cn.bdqn.layui.entity.User;
import cn.bdqn.layui.service.user.UserService;
import cn.bdqn.layui.service.menu.MenuService;
import cn.bdqn.layui.service.role.RoleService;
import org.springframework.http.HttpStatus;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class RouteController {


    @Resource
    RoleService roleService;

    @Resource
    UserService userService;

    @Resource
    MenuService menuService;

    @RequestMapping("/login")
    public String login() {
        return "/views/user/login";
    }

   @RequestMapping("/userfrom")
    public String index() {

        return "/views/user/user/userform";
    }

    @RequestMapping("/console")
    public String console() {
        return "views/home/console";
    }


    @RequestMapping("/user/add")

    public String usertable(Model model){
        List<Role> roleAll = roleService.findRoleAll();
        model.addAttribute("roleAll",roleAll);
        return "/views/user/user/list";
    }

    //角色管理
    @RequestMapping("/quan/jue")
    public String jue(Model model){
        List<Role> roleList = roleService.findRoleAll();
        model.addAttribute("roleList",roleList);
        return "/views/user/administrators/role";
    }


    @RequestMapping("/rolehtml")
    public String rolehtml(Model model){
        List<Role> roleAll = roleService.findRoleAll();
        model.addAttribute("roleAll",roleAll);
        return "/views/user/user/userform.html";
    }

    @RequestMapping("/roleall")
    public String roleall(Model model){
        List<Role> roleAll = roleService.findRoleAll();
        List<Menu> menuList = menuService.findByIdToParentId(0);
        model.addAttribute("roleAll",roleAll);
        model.addAttribute("menuList",menuList);
        return "/views/user/administrators/roleform.html";
    }

    /**
     * 修改用户角色
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/updatarole")
    public String updatarole(Model model,Integer id){
        List<Role> roles = roleService.findRoleAll();
        List<Role> roleList = roleService.findByIdROle(id);
        User user = userService.findByUserName(null, id);
        List<Integer> ridList=new ArrayList<>();
        for (Role role : roleList){
            ridList.add(role.getId());
        }
        model.addAttribute("roles",roles);
        model.addAttribute("ridList",ridList);
        model.addAttribute("user",user);
        return "/views/user/user/userform2.html";
    }

    private RequestCache requestCache = new HttpSessionRequestCache();
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    /**
     * 没有权限进行拦截，返回json数据
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/authentication/require")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public String requireAuthentication(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SavedRequest savedRequest = requestCache.getRequest(request, response);
        if (savedRequest != null) {
            String targetUrl = savedRequest.getRedirectUrl();
            if (StringUtils.endsWithIgnoreCase(targetUrl, ".html"))
                redirectStrategy.sendRedirect(request, response, "/login");
        }
        return "访问的资源需要身份认证！";
    }
}
